
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

public class TestBigData {

	    private static String dir = "/file";
	    private static String targetFile = "/README.txt";
	    private static Configuration conf = new Configuration();

	    static {
	    	String path;
			try {
				path = new File(".").getCanonicalPath();
				File workaround = new File(".");
    			System.getProperties().put("hadoop.home.dir", workaround.getAbsolutePath());
    			/*new File("./bin").mkdirs();
    			new File("./bin/winutils.exe").createNewFile();*/
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        //conf.set("fs.default.name", "hdfs://106.14.126.115:9000");
	    }

	    public static void main(String[] args) throws Exception {
	    	writeFile();
	       //readFile();
	    }

	    public static void writeFile() throws Exception {
	        FileSystem fs = FileSystem.get(conf);

	        //创建目录
	        Path tmpPath = new Path(dir);
	        if (!fs.exists(tmpPath)) {
	            fs.mkdirs(tmpPath);
	        }

	        FSDataOutputStream outputStream = fs.create(new Path(dir + targetFile), true);

	        for (int i = 0; i < 10; i++) {
	            outputStream.writeBytes("the first hdfs write file" + i + "\n");
	        }
	        outputStream.close();
	    }

	    public static void readFile() throws Exception {
	        FileSystem fs = FileSystem.get(conf);

	        Path path = new Path(dir + targetFile);
	        FSDataInputStream inputStream = fs.open(path);

	        BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream));
	        String info = null;
	        while ((info = bf.readLine()) != null) {
	            System.out.println(info);
	        }

	        inputStream.close();
	    }
}
